package com.example.zhangxuemin.fragmenttest130zxm.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.zhangxuemin.fragmenttest130zxm.R;
import com.example.zhangxuemin.fragmenttest130zxm.bean.UserBean;
import com.example.zhangxuemin.fragmenttest130zxm.iface.LoginListener;
import com.example.zhangxuemin.fragmenttest130zxm.model.LoginModel;



    public class LoginActivity extends AppCompatActivity implements View.OnClickListener, LoginListener {
        private EditText username;
        private EditText password;
        private Button btn_login;
        private Button btn_reg;
        private CheckBox remember;
        private SharedPreferences sp;

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            init();
            sp = getSharedPreferences("login", MODE_PRIVATE);
            btn_login.setOnClickListener(this);
            btn_reg.setOnClickListener(this);
            boolean isRemenber = sp.getBoolean("login", false);
            if (isRemenber) {
                String strname = sp.getString("name", null);
                String strpass = sp.getString("pass", null);
                username.setText(strname);
                password.setText(strpass);
                remember.setChecked(true);
            }

        }

        private void init() {
            username = (EditText) findViewById(R.id.et_username);
            password = (EditText) findViewById(R.id.et_password);
            btn_login = (Button) findViewById(R.id.btn_login);
            btn_reg = (Button) findViewById(R.id.btn_register);
            remember = (CheckBox) findViewById(R.id.checkbox_remember);
            sp = getSharedPreferences("login", MODE_PRIVATE);//第一个参数：文件名，第二个参数：模式
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_login:
                    String tmpuser = username.getText().toString();
                    String tmppass = password.getText().toString();
                    LoginModel loginModel = new LoginModel();
                    loginModel.login(tmpuser, tmppass, this);
                    break;
                case R.id.btn_register:
                    Intent intent = new Intent(LoginActivity.this, RegActivity.class);
                    startActivity(intent);
                    finish();

            }
        }


        @Override
        public void onResponse(UserBean userBean) {
            if (userBean.getSessionid() != null) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("sessionid", userBean.getSessionid());
                String strname = username.getText().toString();
                String strpass = password.getText().toString();
                if (remember.isChecked()) {
                    editor.putBoolean("login", true);
                    editor.putString("name", strname);
                    editor.putString("pass", strpass);
                }
                editor.commit();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            } else {
                Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    }
